#!/usr/bin/env python
# Create: 2018/10/25
__author__ = '749B'
"""Twisted"""

from bs4 import BeautifulSoup
from twisted.web.client import getPage, defer
from twisted.internet import reactor

url_list = [
    'https://github.com/explore',
    'https://www.djangoproject.com/',
    'http://www.python-requests.org/en/master/',
    'https://jquery.com/',
    'https://getbootstrap.com/',
    'https://www.solarwinds.com/',
    'https://www.zabbix.com/',
    'http://open-falcon.org/',
    'https://www.python.org/',
    'http://www.jetbrains.com/',
]


def all_done(arg):
    reactor.stop()


def callback(contents):
    soup = BeautifulSoup(contents, features='html.parser')
    title = soup.find('title')
    print(title)


if __name__ == '__main__':
    deferred_list = []
    for url in url_list:
        deferred = getPage(url.encode('utf-8'))  # 发请求
        deferred.addCallback(callback)  # 请求返回后的回调函数
        deferred_list.append(deferred)  # 把所有的请求加到列表里，后面要检测
    dlist = defer.DeferredList(deferred_list)  # 检测所有的请求
    dlist.addBoth(all_done)  # 检测到所有请求都执行完，执行的方法
    reactor.run()  # 开启一个死循环，不停的执行，all_done函数里的stop()方法会停止这个循环

